home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Dev / GNU-SMALLTALK.lha / Point.st < prev    next >
Text File  |  1992-02-15  |  4KB  |  229 lines

  1. "=====================================================================
  2. |
  3. |    Point Class Definitions
  4. |
  5.  ====================================================================="
  6.  
  7. "======================================================================
  8. |
  9. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  10. | By Doug McCallum <uunet!ico.isc.com!dougm>
  11. | Additions by sbb@eng.sun.com (Steve Byrne)
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change
  33. | sbb         10 Jul 91      Added = for Points (= from Object isn't right)
  34. |
  35. | dougm      16 Apr 90    Created basic Point class.
  36. |
  37. "
  38.  
  39. Object subclass: #Point
  40.        instanceVariableNames: 'x y'
  41.        classVariableNames: ''
  42.        poolDictionaries: ''
  43.        category: nil
  44. !
  45.  
  46. Point comment:
  47. 'Beginning of a Point class for simple display manipulation.  Has not been
  48.  exhaustively tested but appears to work for the basic primitives and for
  49.  the needs of the Rectangle class.' !
  50.  
  51. !Number methodsFor: 'point creation'!
  52.  
  53. @ y
  54.     ^ Point x: self y: y
  55. !
  56.  
  57. asPoint
  58.     ^Point x: self y: self
  59. !!
  60.  
  61.  
  62. !Point class methodsFor: 'instance creation'!
  63.  
  64. x: xInteger y: yInteger
  65.     ^self new x: xInteger y: yInteger
  66. !!
  67.  
  68. !Point methodsFor: 'printing'!
  69.  
  70. printOn: aStream
  71.     aStream print: x;
  72.     nextPut: $@;
  73.     print: y
  74. !!
  75.  
  76. !Point methodsFor: 'storing'!
  77.  
  78. storeOn: aStream
  79.     aStream store: x;
  80.     nextPut: $@;
  81.     store: y
  82. !!
  83.  
  84. !Point methodsFor: 'accessing'!
  85.  
  86. x
  87.     ^x
  88. !
  89.  
  90. y
  91.     ^y
  92. !
  93.  
  94. x: aNumber
  95.     x _ aNumber
  96. !
  97.  
  98. y: aNumber
  99.     y _ aNumber
  100. !
  101.  
  102. x: anXNumber y: aYNumber
  103.     x _ anXNumber.
  104.     y _ aYNumber
  105. !!
  106.  
  107. !Point methodsFor: 'converting'!
  108.  
  109. asPoint
  110.     ^self            "But I already AM a point!"
  111. !
  112.  
  113. hash
  114.     ^(x abs + y abs) truncated
  115. !!
  116.  
  117. !Point methodsFor: 'arithmetic'!
  118.  
  119. + delta
  120.     delta _ delta asPoint.
  121.     ^Point x: (x + delta x) y: (y + delta y)
  122. !
  123.  
  124. - delta
  125.     delta _ delta asPoint.
  126.     ^Point x: (x - delta x) y: (y - delta y)
  127. !
  128.  
  129. * scale
  130.     scale _ scale asPoint.
  131.     ^Point x: (x * scale x) y: (y * scale y)
  132. !
  133.  
  134. / scale
  135.     scale _ scale asPoint.
  136.     ^Point x: (x / scale x) y: (y / scale y)
  137. !
  138.  
  139. // scale
  140.     scale _ scale asPoint.
  141.     ^Point x: (x // scale x) y: (y // scale y)
  142. !
  143.  
  144. abs
  145.     ^Point x: (x abs) y: (y abs)
  146. !!
  147.  
  148. !Point methodsFor: 'truncation and round off'!
  149. rounded
  150.     ^Point x: (x rounded) y: (y rounded)
  151. !
  152.  
  153. truncateTo: grid
  154.     ^Point x: (x truncateTo: grid) y: (y truncateTo: grid)
  155.  
  156. !!
  157.  
  158.  
  159. !Point methodsFor: 'comparing'!
  160.  
  161. = aPoint
  162.     ^(x = aPoint x) and: [ y = aPoint y ]
  163. !
  164.  
  165. < aPoint
  166.     ^(x < aPoint x) and: [ (y < aPoint y) ]
  167. !
  168.  
  169. > aPoint
  170.     ^(x > aPoint x) and: [ (y > aPoint y) ]
  171. !
  172.  
  173. <= aPoint
  174.     ^(self > aPoint) not    "unverified"
  175. !
  176.  
  177. >= aPoint
  178.     ^(self < aPoint) not    "unverified"
  179. !
  180.  
  181. max: aPoint
  182.     (self >aPoint )
  183.        ifTrue: [ ^self ]
  184.        ifFalse:[ ^aPoint ]
  185. !
  186.  
  187. min: aPoint
  188.     (self < aPoint)
  189.        ifTrue: [^ self ]
  190.        ifFalse:[^ aPoint ]
  191. !!
  192.  
  193. !Point methodsFor: 'point functions'!
  194.  
  195. dist: aPoint
  196.     | a b |
  197.     a _ x - (aPoint x).
  198.     b _ y - (aPoint y).
  199.     ^((a squared) + (b squared)) sqrt
  200. !
  201.  
  202. dotProduct: aPoint
  203.     ^(x * aPoint x) + (y * aPoint y)
  204. !
  205.  
  206. grid: aPoint
  207.     ^Point x: (x roundTo: (aPoint x)) y: (y roundTo: (aPoint y))
  208. !
  209.  
  210. normal
  211. "rotate the Point 90degrees clockwise and get the unit vector"
  212.     |len|
  213.     len _ ((x squared) + (y squared)) sqrt.
  214.     ^Point x: ((y asFloat negated)/len) y: (x/len)
  215. !
  216.  
  217. transpose
  218.     ^Point x: y y: x
  219. !
  220.  
  221. truncatedGrid: aPoint
  222.     ^Point x: (x truncateTo: (aPoint x)) y: (y truncateTo: (aPoint y))
  223. !!
  224.  
  225.  
  226.  
  227.